package com.polaris.live.adapter.gift

import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.RecycledViewPool
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.resp.back_resp.GiftBean

/**
 * GiftPagerAdapter
 *
 * @author Created by lucas on 2023/11/16 13:51
 */
class GiftPagerAdapter : BaseQuickAdapter<List<GiftBean>, GiftPagerAdapter.ViewHolder>(0) {

    private var mSelectIndex: Int = -1
    private var mSelectPosition: Int = -1
    private var mSelectTimes = 1

    private var mGiftClickListener: OnGiftClickListener? = null

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(RecyclerView(context))
    }

    override fun convert(holder: ViewHolder, item: List<GiftBean>) {
        holder.adapter.setList(item)

        if (holder.layoutPosition == mSelectIndex) {
            holder.adapter.updateSelected(mSelectPosition, mSelectTimes)
        } else {
            holder.adapter.updateSelected(-1, mSelectTimes)
        }
        holder.adapter.setOnItemClickListener { _, _, position ->
            mGiftClickListener?.onItemClick(holder.adapter, holder.layoutPosition, position)
        }
    }

    fun getHolder(position: Int): ViewHolder? {
        val holder = recyclerView.findViewHolderForAdapterPosition(position)
        return if (holder is ViewHolder) {
            holder
        } else {
            null
        }
    }

    fun getAdapter(position: Int): GiftWallAdapter? {
        return getHolder(position)?.adapter
    }

    fun updateIndex(index: Int, position: Int) {
        if (mSelectIndex != -1) {
            notifyItemChanged(mSelectIndex)
        }

        mSelectIndex = index
        mSelectPosition = position

        if (index != -1) {
            notifyItemChanged(index)
        }
    }


    fun updateTimes(times: Int) {
        mSelectTimes = times

        if (mSelectIndex != -1) {
            notifyItemChanged(mSelectIndex)
        }
    }

    fun reset() {
        if (mSelectIndex != -1) {
            notifyItemChanged(mSelectIndex)
        }

        mSelectIndex = 0
        mSelectPosition = 0
        mSelectTimes = 1
    }

    fun getIndexPage(): Int {
        return mSelectIndex
    }

    fun getSelectTimes(): Int {
        return mSelectTimes
    }


    fun getSelectPosition(): Int {
        return mSelectPosition
    }

    class ViewHolder(view: RecyclerView) : BaseViewHolder(view) {

        val adapter: GiftWallAdapter = GiftWallAdapter()

        init {
            view.setRecycledViewPool(RecycledViewPool().apply {
                setMaxRecycledViews(0, 10)
            })

            view.layoutParams = ViewGroup.LayoutParams(
                RecyclerView.LayoutParams.MATCH_PARENT,
                RecyclerView.LayoutParams.MATCH_PARENT
            )
            view.layoutManager =
                GridLayoutManager(view.context, 4, GridLayoutManager.VERTICAL, false)
            view.adapter = adapter
            view.itemAnimator = null
        }
    }

    fun setOnGiftItemClickListener(l: OnGiftClickListener) {
        mGiftClickListener = l
    }
}

fun interface OnGiftClickListener {

    fun onItemClick(adapter: GiftWallAdapter, index: Int, position: Int)

}
